package asia.doing.service.admin;

import asia.doing.common.utils.UUIDUtils;
import cn.hutool.core.util.ObjectUtil;
import asia.doing.common.Constants;
import asia.doing.common.enums.ResultCodeEnum;
import asia.doing.common.enums.RoleEnum;
import asia.doing.entity.Account;
import asia.doing.entity.User;
import asia.doing.common.exception.CustomException;
import asia.doing.mapper.UserMapper;
import asia.doing.common.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service(value = "manageUserService")
public class ManageUserService {
    @Resource
    private UserMapper userMapper;

    public void add(User user){
        //判断用户是否重复
        User dbUser = userMapper.selectByUsername(user.getUsername());
        if(dbUser != null){
            throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
        }
        //判断用户密码是不是为空
        if(ObjectUtil.isEmpty(user.getPassword())){
            user.setPassword(Constants.USER_DEFAULT_PASSWORD);
        }
        //判断用户名称是否为空
        if(ObjectUtil.isEmpty(user.getUsername())){
            user.setName(user.getUsername());
        }
        //默认添加新的用户
        user.setRole(RoleEnum.USER.name());
        // 使用uuid作为唯一性的id进行插入数据
        user.setId(UUIDUtils.getUUIDInOrderId());
        userMapper.insert(user);
    }

    public void deleteById(Long id){
        userMapper.deleteById(id);
    }

    public void deleteBatch(List<Long> ids){
        for(Long id : ids) {
            this.deleteById(id);
        }
    }

    public void updateById(User user){
        userMapper.updateById(user);
    }

    public User selectById(Long id){
        return userMapper.selectById(id);
    }

    public List<User> selectAll(User user){
        return userMapper.selectAll(user);
    }

    public PageInfo<User> selectPage(User user, Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userMapper.selectAll(user);
        return PageInfo.of(list);
    }

    /**
     * 登录
     */
    public Account login(Account account) {
        System.out.println(account.getUsername());
        Account dbUser = userMapper.selectByUsername(account.getUsername());
        System.out.println(dbUser.getUsername());
        if (ObjectUtil.isNull(dbUser)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        if (!account.getPassword().equals(dbUser.getPassword())) {
            throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
        }
        // 生成token
        String tokenData = dbUser.getId() + "-" + RoleEnum.USER.name();
        String token = TokenUtils.createToken(tokenData, dbUser.getPassword());
        System.out.println("登录的时候我生成了token" + token);
        dbUser.setToken(token);
        return dbUser;
    }

    /**
     * 注册
     */
    public void register(Account account) {
        User user = new User();
        BeanUtils.copyProperties(account, user);
        add(user);
    }

    /**
     * 修改密码
     */
    public void updatePassword(Account account) {
        User dbUser = userMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(dbUser)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        if (!account.getPassword().equals(dbUser.getPassword())) {
            throw new CustomException(ResultCodeEnum.PARAM_PASSWORD_ERROR);
        }
        dbUser.setPassword(account.getNewPassword());
        userMapper.updateById(dbUser);
    }
}
